package club.babyf.nativeai.service.impl;

import club.babyf.nativeai.common.exception.BusinessException;
import club.babyf.nativeai.common.exception.ErrorCode;
import club.babyf.nativeai.service.IFileParserService;
import club.babyf.nativeai.utils.FileParser;
import club.babyf.nativeai.utils.TextSplitter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * FileParserServiceImpl.
 *
 * @author zhanglei.
 * @date 2025/3/17 10:17.
 * @description 文档解析服务实现.
 */
@Slf4j
@Service
public class FileParserServiceImpl implements IFileParserService {

    @Override
    public String parse(MultipartFile file) {
        try {
            return FileParser.parseFile(file);
        } catch (IOException e) {
            throw new BusinessException(e.getMessage(), ErrorCode.PARAMS_ERROR.getCode());
        }
    }

    @Override
    public List<String> parseSplit(MultipartFile file) {
        String text;
        try {
            text = FileParser.parseFile(file);
        } catch (IOException e) {
            throw new BusinessException(e.getMessage(), ErrorCode.PARAMS_ERROR.getCode());
        }
        return TextSplitter.splitIntoSensibleChunks(text, 500, 100);
    }

}
